package com.colin.app.activity.senior.wifi;

import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pManager;
import android.view.Menu;
import android.view.MenuItem;

import com.colin.app.R;
import com.colin.app.activity.senior.SeniorContract;
import com.colin.app.activity.senior.SeniorPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.app.broadcast.WiFiDirectReceiver;
import com.colin.library.help.LogHelp;
import com.colin.library.other.Url;

public class WifiActivity extends AppActivity implements SeniorContract.SeniorView {
    private SeniorContract.SeniorPresenter mSeniorPresenter = null;

    private WifiP2pManager mManager;
    private WifiP2pManager.Channel mChannel;
    private IntentFilter directFilter;
    private WiFiDirectReceiver directReceiver;
    private WifiP2pConfig mConfig = new WifiP2pConfig();

    @Override
    protected void onDestroy() {
        if (null != mSeniorPresenter) {
            mSeniorPresenter.destroy();
            mSeniorPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mSeniorPresenter = new SeniorPresenterImpl(this, this, demoBean);
    }


    @Override
    public int initViewLayoutID() {
        return R.layout.activity_wifi;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        mSeniorPresenter.setData();
        initWiFi();
    }

    /**
     * 初始化WiFi 动态注册相关广播
     */
    private void initWiFi() {
        //获取WiFi管理服务
        mManager = (WifiP2pManager) this.getSystemService(Context.WIFI_P2P_SERVICE);
        //初始化WiFi频道 触发
        //WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION的广播，
        //在BroadcastReceiver中，如果WifiP2pManager.WIFI_P2P_STATE_ENABLED的状态已经打开，
        //在BroadcastReceiver调用discoverPeers方法,如果发现设备会回调onSuccess方法
        mChannel = mManager.initialize(this, this.getMainLooper(), null);

        directReceiver = new WiFiDirectReceiver(mManager, mChannel, this);

        directFilter = new IntentFilter();
        directFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        directFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        directFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        directFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
        //注册广播监听
        this.registerReceiver(directReceiver, directFilter);

    }

    @Override
    public void initListener() {
        mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                LogHelp.e("discovery success");
            }

            @Override
            public void onFailure(int reason) {
                LogHelp.e("discovery failed");
            }
        });

        //发现设备，系统会触发WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION这个广播，
        //在这个广播中可以调用requestPeers方法可以列出所有的设备
        if (null != mManager) {
            mManager.requestPeers(mChannel, new WifiP2pManager.PeerListListener() {
                @Override
                public void onPeersAvailable(WifiP2pDeviceList peers) {
                    LogHelp.e("peers list size: " + peers.getDeviceList().size());
                    for (WifiP2pDevice device : peers.getDeviceList()) {
                        LogHelp.e("device addr: " + device.deviceName + " name: " + device.deviceName);
                    }
                }
            });
        }



//        mConfig.deviceAddress = device.deviceAddress;
//        mManager.connect(mChannel, mConfig, new WifiP2pManager.ActionListener() {
//            @Override
//            public void onSuccess() {
//                LogHelp.e("connect success");
//            }
//
//            @Override
//            public void onFailure(int reason) {
//                LogHelp.e("connect failed");
//            }
//        });

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        //取消注册
        this.unregisterReceiver(directReceiver);
        super.onPause();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_to_url, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_to_web:
                mSeniorPresenter.toWebViewActivity(Url.URL_WIFI);
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return super.onOptionsItemSelected(item);
    }
}
